/** @file GOTHad.h This file contains the API for the ADC and comparator modules of the GOTH
 * Kick-ass Etherboard.
 * Currently, 3 available analog channels of the board are used (AN2, AN4 & AN5 these are RB2,
 * RB4 & RB5 resp.).
 * The RB4 pin can be connected to the on-board temperature sensor.
 *
 * Use AD_AdcMeasure() to measure all the channels and the use AD_AdcGetValue(ADC_CHANNEL_X)
 * to get the decimal value representing the voltage.
 * The voltage can be calculated using this formula:
 *	voltage = (3.3 * AD_AdcGetValue(ADC_CHANNEL_X)) / 1024
 *
 * The comparator has two analog channels (C2INC and C2IND, RG9 and RG8 resp.). They can be
 * compared against a third channel (C2INA, RB3 resp.).
 *
 * @author Geoffrey Ottoy - Tom Hamelinckx
 * @date 05/07/2011 (last modified)
 *
 * @todo add code for reading out temperature or exact voltages in stead of the 10-bit adc value.
 *
 * <pre>
 * MODIFICATION HISTORY:
 *
 * Who  Date     Changes
 * ---- -------- ---------------------------------------------------------
 * GO   01/07/11 Created this file.
 * GO   04/07/11 Implemented and tested AD_InitAdc(), AD_AdcMeasure() and
 *               AD_AdcGetValue().
 * GO	04/07/11 Added and tested AD_InitComparator(), AD_CompareA2C() and
 *               AD_CompareA2D().
 * TH	05/07/11 Corrected some minor issues.
 * </pre>
 * 
 */

#ifndef __GOTH_AD_H__
#define __GOTH_AD_H__

#include "p24fj256gb110.h"
#include "GOTHglobal.h"

#define ADC_NR_OF_CHANNELS	3
#define ADC_CHANNEL_2		0
#define ADC_CHANNEL_4		1
#define ADC_CHANNEL_5		2

#define AD_AdcOff() {AD1CON1bits.ADON = 0;}
#define AD_AdcOn() {AD1CON1bits.ADON = 1;}
#define AD_ComparatorOff() {CM2CONbits.CON = 0;}
#define AD_ComparatorOn() {CM2CONbits.CON = 1;}

/** Initialize the comparator module for operation.
 *	2 Channels (C2INC & C2IND) can be compared against another channel (C2INA).
 *	Currently, the comparator works in a polled mode (so no interrupt on output change of 
 *	the comparator).
 *
 * @return nothing
 */
void AD_InitComparator();

/** Compare C2INA with C2INC
 *
 * @return 0 if C2INA < C2INC, else 1
 */
int AD_CompareA2C();

/** Compare C2INA with C2IND
 *
 * @return 0 if C2INA < C2IND, else 1
 */
int AD_CompareA2D();

/** Initialize the ADC module for operation.
 *		3 Channels are used: AN2, AN4, AN5.
 *
 * @return nothing
 */
void AD_InitAdc();

/** Perform a measurement (scan the selected channels and store the result).
 *
 * @return nothing
 */
void AD_AdcMeasure();

/** Get the measured value (0-1023) for a requested channel.
 * Voltage = (3.3 * adc_result) / 1024
 *
 * @return the requested value
 */
int AD_AdcGetValue(int channel);

#endif //__GOTH_AD_H__
